package com.yykt.myapp.controller;

import com.yykt.myapp.config.GlobalUser;
import com.yykt.myapp.util.JsonResult;
import com.yykt.myapp.vo.ApiUser;
import com.yykt.myapp.vo.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RequestMapping("myapp")
@RestController
public class Demo {

    @GetMapping("/demo")
        // GET 请求参数的接受：defaultValue require value(映射前端传递的参数)
    JsonResult demo(@RequestParam(value = "user_name", required = true, defaultValue = "sdx") String name) {
        // jackson json 序列化库， 对象转jsonstring , json string 转对象
        return JsonResult.success(name);
    }

    @PostMapping("demo_post")
    JsonResult demoPost(@RequestBody @Valid User user, BindingResult bindingResult) {

        if (bindingResult.hasErrors()) {
            Map<String, String> map = new HashMap<>();
            for (FieldError error : bindingResult.getFieldErrors()) {
                map.put(error.getField(), error.getDefaultMessage());
            }
            return JsonResult.error(map);
        }
        return JsonResult.success(user);
    }


    @Autowired
    private GlobalUser globalUser;

    @PostMapping("api/v1/getData")
    JsonResult api(@RequestBody ApiUser user) {

        //  String ak = superAdminConfigProperties.getAk();
        // String sk = superAdminConfigProperties.getSk();

        String userAk = user.getAk();
        String userSk = user.getSk();

        String sysSk = globalUser.globalMap.get(userAk);

        if (sysSk.equals(userAk)) {
            // 生成一个session
            return JsonResult.success("正确登录");
        } else {
            return JsonResult.error("错误登录");
        }
    }

    /**
     * 登录接口：http://127.0.0.1:8081/myapp/login?username=sdx&password=sdx
     * @param username
     * @param password
     * @param httpServletRequest
     * @return
     */
    @GetMapping("/login")
    JsonResult login(
        @RequestParam(value = "username") String username,
        @RequestParam(value = "password") String password,
        HttpServletRequest httpServletRequest
    ) {
        if ("sdx".equals(username) && "sdx".equals(password)) {
            HttpSession session = httpServletRequest.getSession();
            session.setAttribute("user", "sdx");
            return JsonResult.success("登录成功");
        }
        return JsonResult.error("登录失败");
    }

    /**
     * 校验是否登录接口: http://127.0.0.1:8081/myapp/check_login
     * @param httpServletRequest
     * @return
     */
    @GetMapping("/check_login")
    JsonResult checkLogin(
        HttpServletRequest httpServletRequest
    ) {
        HttpSession session = httpServletRequest.getSession(false);
        if (session == null) {
            return JsonResult.error("未登录");
        }
        String user = (String) session.getAttribute("user");
        if (user.length() <= 0) {
            return JsonResult.error("未登录");
        }
        return JsonResult.success("已经登录" + user);
    }

    /**
     * 登出接口
     * @param httpServletRequest
     * @return
     */
    @GetMapping("/logout")
    JsonResult logout(
        HttpServletRequest httpServletRequest
    ) {

        HttpSession session = httpServletRequest.getSession(false);
        if (session != null) {
            session.invalidate();
        }
        return JsonResult.success("登出成功");
    }


}
